package com.sirun.cn.fca.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sirun.cn.fca.R;
import com.sirun.cn.fca.presenter.IHomePresenter;
import com.sirun.cn.fca.presenter.impl.HomePresenter;
import com.sirun.cn.fca.view.IHomeFlowView;
import com.sirun.cn.fca.widget.ProcessView2;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.IovKernel;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.ui.widget.IovButton;
import com.tencent.cloud.uikit.utils.DialogUtils;

import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.constant.ControlIntentConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.account.uitl.CurCarInfoUtils;
import cn.sirun.com.fca.account.uitl.PersonalInfoUtils;
import cn.sirun.com.fca.personal.center.model.bean.CarListResponse;

public class HomeFlowView extends BaseLoadingFlowView<IHomePresenter> implements IHomeFlowView
        , View.OnClickListener {
    private IovButton mMessageBtn;
    private RelativeLayout mAuthRL, mUnauthRL;
    private TextView mAuthTV;
    private TextView mLocationTV, mTimeTV;
    private LinearLayout mBeerLL, mLockLL, mUnlockLL, mEngineOnLL, mEngineOffLL, mMessageLL;
    private ProcessView2 processView2;

    private String mCurDin;

    public HomeFlowView(Activity activity) {
        super(activity);
    }

    public HomeFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onGetCarListResult(BaseResponse<CarListResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            return;
        }
        hideLoadingView();
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    public void onPersonalInfoResult(BaseResponse<PersonalInfo> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            PersonalInfo personalInfo = response.getData();
            if (!TextUtils.isEmpty(personalInfo.getIovCurrentVehicle())) {
                TXSharedPreferencesUtils.setValue(CarConst.SpKey.CAR_ACTIVATE_INFO,
                        personalInfo.getIovCurrentVehicle());
                mCurDin = personalInfo.getIovCurrentVehicle();
                PersonalInfoUtils.savePersonalInfo(personalInfo);
                getPresenter().getCarInfo(personalInfo.getIovCurrentVehicle());
            } else {
                mAuthRL.setVisibility(View.GONE);
                mUnauthRL.setVisibility(View.VISIBLE);
                mMessageLL.setVisibility(View.INVISIBLE);
            }
            return;
        }
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    public void onCarInfoResult(BaseResponse<CarInfoDetailResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            CarInfoDetailResponse carInfo = response.getData();
            int availableStatus = carInfo.getAvailableStatus();
            if (availableStatus == 0) {
                mAuthRL.setVisibility(View.GONE);
                mUnauthRL.setVisibility(View.VISIBLE);
                mMessageLL.setVisibility(View.INVISIBLE);
            } else if (availableStatus == 1) {
                mAuthRL.setVisibility(View.GONE);
                mUnauthRL.setVisibility(View.VISIBLE);
                mAuthTV.setVisibility(View.INVISIBLE);
                mMessageLL.setVisibility(View.INVISIBLE);
            }
            CurCarInfoUtils.saveCarInfo(carInfo);
            if (carInfo.getReseller() != null) {
                PersonalInfo personalInfo = PersonalInfoUtils.getPersonalInfo();
                personalInfo.setReseller(carInfo.getReseller());
                PersonalInfoUtils.savePersonalInfo(personalInfo);
            }
            return;
        }
        hideLoadingView();
        DialogUtils.showToast(getActivity(), response.getMsg());
    }


    @Override
    protected IHomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.home_layout, null, false);
        setContentView(view);
        view.setBackgroundResource(cn.sirun.com.fca.personal.center.R.drawable.page_bg_gradient);

        mMessageBtn = view.findViewById(R.id.btn_message);
        mAuthRL = view.findViewById(R.id.rl_auth);
        mUnauthRL = view.findViewById(R.id.rl_unauth);
        mAuthTV = view.findViewById(R.id.bt_go_activation);

        mLocationTV = view.findViewById(R.id.tv_control_location);
        mTimeTV = view.findViewById(R.id.tv_location_time);
        mBeerLL = view.findViewById(R.id.ll_control_beer);
        mLockLL = view.findViewById(R.id.ll_control_lock);
        mUnlockLL = view.findViewById(R.id.ll_control_unlock);
        mEngineOnLL = view.findViewById(R.id.ll_control_engine_on);
        mEngineOffLL = view.findViewById(R.id.ll_control_engine_off);
        mMessageLL = view.findViewById(R.id.ll_message);
        processView2 = view.findViewById(R.id.process_car_oil);
        processView2.setNum(100f);
        mAuthTV.setOnClickListener(this);
        mBeerLL.setOnClickListener(this);
        mLockLL.setOnClickListener(this);
        mUnlockLL.setOnClickListener(this);
        mEngineOnLL.setOnClickListener(this);
        mEngineOffLL.setOnClickListener(this);

    }

    private void initData() {
        PersonalInfo personalInfo = PersonalInfoUtils.getPersonalInfo();
        if (personalInfo != null) {
            if (!TextUtils.isEmpty(personalInfo.getIovCurrentVehicle())) {
                CarInfoDetailResponse carInfo = CurCarInfoUtils.getCarInfo();
                if (carInfo != null) {
                    int availableStatus = carInfo.getAvailableStatus();
                    if (availableStatus == 0) {
                        mAuthRL.setVisibility(View.GONE);
                        mUnauthRL.setVisibility(View.VISIBLE);
                        mMessageLL.setVisibility(View.INVISIBLE);
                    } else if (availableStatus == 1) {
                        mMessageLL.setVisibility(View.INVISIBLE);
                        mAuthRL.setVisibility(View.GONE);
                        mUnauthRL.setVisibility(View.VISIBLE);
                        mAuthTV.setVisibility(View.INVISIBLE);
                    }
                }
            } else {
                mAuthRL.setVisibility(View.GONE);
                mUnauthRL.setVisibility(View.VISIBLE);
                mMessageLL.setVisibility(View.INVISIBLE);
            }
        } else {
            showLoadingView();
            PersonalInfoRequest request = new PersonalInfoRequest(
                    IovKernel.getQQAppId(), IovKernel.getWeChatAppId()
            );
            getPresenter().getPersonalInfo(request);
        }
    }

    private void pinIntent(int type) {
        if (!TextUtils.isEmpty(mCurDin)) {
            String path = AccountActivityConst.Path.PIN_VALIDATE;
            Intent pinIntent = Router.fromPath(path);
            pinIntent.putExtra(AccountIntentConst.FROM, AccountIntentConst.CONTROL_CAR_VERIFY_PIN);
            pinIntent.putExtra(ControlIntentConst.CONTROL_TYPE, type);
            getActivity().startActivity(pinIntent);
        } else {
            DialogUtils.showToast(getActivity(), "暂无控制车辆");
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_go_activation) {
            if (!TextUtils.isEmpty(mCurDin)) {
                Intent intent = Router.fromPath(AccountActivityConst.Path.USER_ARGEEMENT);
                intent.putExtra("din", mCurDin);
                ActivityUtils.startActivityForResult(getActivity(), intent);
            } else {
                DialogUtils.showToast(getActivity(), "暂无控制车辆");
            }
        } else if (id == R.id.ll_control_beer) {
            pinIntent(ControlIntentConst.BEER);
        } else if (id == R.id.ll_control_lock) {
            pinIntent(ControlIntentConst.LOCK);
        } else if (id == R.id.ll_control_unlock) {
            pinIntent(ControlIntentConst.UNLOCK);
        } else if (id == R.id.ll_control_engine_on) {
            pinIntent(ControlIntentConst.ENGINE_ON);
        } else if (id == R.id.ll_control_engine_off) {
            pinIntent(ControlIntentConst.ENGINE_OFF);
        }
    }
}
